package cn.zhl.boot.controller;

import cn.zhl.boot.service.SqlService;
import cn.zhl.boot.wsdl.a5.WebService1Soap;
import com.szy.scecpal.EquipQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-06-05 11:23
 **/
@RestController
@Slf4j
public class WebServiceController {
//	@Autowired
//	private TestA5DmgcYJbService testA5DmgcYJbService;
	@Autowired
	private WebService1Soap webService1Soap;
	@Autowired
	private SqlService sqlService;
	@Autowired
	private EquipQueryService equipQueryService;

	@GetMapping(value = "/equip",produces = MediaType.APPLICATION_XML_VALUE)
	public Object equip(@RequestParam(name = "func",required = true)String func,
						@RequestParam(name = "beginDate",required = false)String beginDate,
						@RequestParam(name = "endDate",required = false)String endDate){
		RestTemplate rest = new RestTemplate();
		String uri = "http://10.79.1.204/services/EquipQueryService?username=wlw&password=zf@123456";
		String capsule ="<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://service.tlm.webservice.creatsoft.com/\">   <soap:Header/>   <soap:Body> %s </soap:Body> </soap:Envelope>";

		String req = "<ser:"+func+"> <arg0> &lt;creatsoft&gt;" +
			"&lt;param name=\"beginDate\" value=\""+beginDate+"\" /&gt;" +
			"&lt;param name=\"endDate\" value=\""+endDate+"\" /&gt;" +
			"&lt;/creatsoft&gt;</arg0> </ser:"+func+">";
//		String req = "<ser:getPipe> <arg0> &lt;creatsoft&gt;&lt;param name=\"管道编号\" value=\"TH-HF-NS-GC-0107\" /&gt;&lt;/creatsoft&gt;</arg0> </ser:getPipe>";
		String format = String.format(capsule, req);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));

		HttpEntity<String> httpEntity = new HttpEntity(format, headers);
		ResponseEntity<String> response = new RestTemplate().postForEntity(uri, httpEntity, String.class);
//		System.out.println(response);
		System.out.println(format);
		System.out.println(response);
		return response.getBody();
	}

	@GetMapping(value = "/yygx",produces = MediaType.APPLICATION_XML_VALUE)
	public Object yygx(){
		String xml = webService1Soap.dmgcYYGX("46jw8l8ym", "1988-1-1");
		System.out.println(xml);
		sqlService.printSql("TEST_A5_DMGC_Y_YGX",xml);
		return xml;
	}

	@GetMapping(value = "/jrl",produces = MediaType.APPLICATION_XML_VALUE)
	public Object jrl(){
		String xml = webService1Soap.dmgcJRL("46jw8l8ym", "1988-1-1");
		return xml;
	}

	@GetMapping(value = "/dmgcYJB",produces = MediaType.APPLICATION_XML_VALUE)
	public Object getSql(@RequestParam(name = "data",required = false,defaultValue = "1988-1-1")
							 String data,
						 @RequestParam(name = "key",required = false,defaultValue = "46jw8l8ym")
							String key
						 ){
		String xml = webService1Soap.dmgcYJB(key, data);
		sqlService.printSql("TEST_A5_DMGC_Y_JB",xml);
		return xml;
	}

	@GetMapping(value = "/webservice",produces = MediaType.APPLICATION_XML_VALUE)
	public Object test1(@RequestParam(name = "data",defaultValue = "1980-1-1",required = false)
							String data){
		String xml = webService1Soap.dmgcYJB("46jw8l8ym", data);
//		saveXml2Bean(xml);
		return xml;
	}

//	private void saveXml2Bean(String xml){
//		TableList tableList = Xml2Bean.converyToJavaBean(xml, TableList.class);
//		List<TestA5DmgcYJbEntity> entitys = new ArrayList<>();
//		tableList.getTables().forEach(ex->{
//			try {
//				TestA5DmgcYJbEntity target = new TestA5DmgcYJbEntity();
//				ReflectUitl.reflectionAttr(ex,target);
//				entitys.add(target);
//			}catch (Exception e){
//				e.printStackTrace();
//			}
//		});
//		testA5DmgcYJbService.saveBatch(entitys,500);
//		System.out.println("===========end==============size:"+tableList.getTables().size());
//	}





	@GetMapping("/testSave")
	public void testSave(){

//		List<TestA5DmgcYJbEntity> list = new ArrayList();
//		TestA5DmgcYJbEntity en = new TestA5DmgcYJbEntity();
//		en.setDjglys(10);
//		en.setEventid("abc");
//
//		TestA5DmgcYJbEntity en1 = new TestA5DmgcYJbEntity();
//		en1.setDjglys(20);
//		en1.setEventid("abcd");
//		list.add(en);
//		list.add(en1);
//		testA5DmgcYJbService.saveBatch(list,10);
	}
}
